local TD = TD.td1madao["kiana_kaslana_td1madao"]
local inst = _G.ThePlayer
local isChinese = TD.isCh()
local currentAddress = string.format(TD.gs(2), TD.CHAR)
local myModUrl = "http://steamcommunity.com/sharedfiles/filedetails/?id=847991972"
local workshopNames = { "workshop-847991972" }
local isTGP = TheSim.RAILGetPlatform ~= nil and (TheSim:RAILGetPlatform() == "TGP")
local isPiracy = TheNet:GetCountryCode() == "US" and string.find(string.upper(STRINGS.NAMES.GRASS), "草") ~= nil and not isTGP

local function createWindow(title, content, tab)
    local mywindow = (require(TD.gs(9)))(title, content, tab)
    _G.TheFrontEnd:PushScreen(mywindow)
    mywindow.bg:SetSize(500, 400)
    mywindow.bg.fill:SetSize(1000, 700)
    mywindow.title:SetPosition(0, 200, 0)
    mywindow.text:SetRegionSize(900, 500)
    mywindow.menu:SetPosition(mywindow.menu:GetPosition().x, -180, 0)
end

local function saveNewAd(content)
    TD.csv(inst):value("adcontext", content, true)
end

local function needAdUpdate(content)
    return TD.csv(inst):get("adcontext") ~= content
end

local function runForServer(command)
    local mySender = {}
    mySender.serverCommand = command
    TD.serverCall(TD.holder("%s_remoteCommand"), mySender)
end

local function requireAfter(time)
    TD.latter(inst, time, function()
        TD.runRess(currentAddress)
    end)
end

if TD.IsWorkshopMod(TD.MODNAME) and not isPiracy then
    if not table.td1madao_contain(workshopNames, TD.MODNAME) or isTGP then
        local title = "td1madao:Found a malicious upload"
        local content = string.format("Your current play is someone else tampering after uploading my mod \n Please uploader timely withdraw this mod \n to avoid disputes \n genuine address:%s", myModUrl)
        local yes = "Subscribe to genuine"
        if isChinese then
            title = "天地:发现恶意上传"
            content = string.format("你当前玩的是别人篡改后私自上传我的mod\n请恶意上我mod的人及时删掉该mod\n避免引起纠纷\n正版地址:%s", myModUrl)
            yes = "订阅正版"
        end
        local function warningMalicious()
            createWindow(title, content, {
                {
                    text = yes,
                    cb = function()
                        _G.VisitURL(myModUrl)
                        _G.TheFrontEnd:PopScreen()
                    end
                }
            })
        end

        warningMalicious()
        TD.cpr(inst, function(comp)
            comp:common_addTask(TD.holder("td1madao%sWarnTask"), warningMalicious, 60)
            comp:common_addTask(TD.holder("td1madao%sWarnTask2"), function()
                inst.components.talker:Say(content, 5, true, true, false, { 1, 0, 0, 1 })
            end, 10)
        end)
        TD.latter(inst, 60 * 8, function()
            inst[TD.actVis()] = nil
            inst[TD.stUpC()] = nil
        end)
    end
else
    local title = "td1madao:Found piracy"
    local content = string.format("This is someone else carrying my mod \n Please do not arbitrarily carry my mod \n If it is pirated players please try to support steam or tgp genuine \n genuine address:%s", myModUrl)
    local yes = "Subscribe to genuine"
    local ve = "piracy version"
    local addition = ""
    if isPiracy then
        addition = "[piracy DST]"
    elseif isTGP then
        addition = "[TGP]"
    else
        addition = "[steam]"
    end
    if isChinese then
        title = "天地:发现搬运"
        content = string.format("这个是别人私自搬运了我的mod\n请不要随意搬运我的mod\n如果是盗版玩家请尽量支持steam或者tgp正版\n正版地址:%s", myModUrl)
        yes = "订阅正版"
        ve = "搬运版"
        if isPiracy then
            addition = "[盗版饥荒]"
        elseif isTGP then
            addition = "[TGP]"
        else
            addition = "[steam]"
        end
    end
    createWindow(title, content, {
        {
            text = yes,
            cb = function()
                _G.VisitURL(myModUrl)
                _G.TheFrontEnd:PopScreen()
            end
        }
    })
    local Text = require "widgets/text"
    local sb = inst.HUD.controls.sidepanel
    local a = sb:AddChild(Text(DEFAULTFONT, 45, (ve .. addition), { 1, 0, 0, 1 }))
    a:SetPosition(-600, 0, 0)
end